package com.gec.service.Impl;

import com.gec.dao.RoomDao;
import com.gec.domain.*;
import com.gec.service.abst.RoomService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl extends BaseService implements RoomService {
    private final RoomDao roomDao;

    public RoomServiceImpl(RoomDao roomDao) {
        this.roomDao = roomDao;
    }

    //+-----------------------------------Room--------------------------------------------+
    @Override
    public PageBean<Room> getRoomList(PageParam param) {
        int size = param.getLimit();  //页大小
        int page = param.getPage();   //页码
        //{1}在查询之前先调用这个方法来准备拦截查询
        Page<Object> pageInfo = PageHelper.startPage(page, size, true);
        //{2}调用 dao 方法来查询
        List<Room> list = roomDao.getRoomList(param.getKeywords());
        PageBean pBean = new PageBean();
        int total = (int) pageInfo.getTotal();
        //{3}生成 PageBean, 再封装信息到 pBean
        pBean.setCount(total);  //总记录数
        pBean.setData(list);    //用户列表
        pBean.setCode(0);     //状态码
        return pBean;
    }

    @Override
    public void saveRoom(Room room) throws Exception {
        String id = room.getId();
        try {
            if (id != null) { // id!=null --> 更新
                System.out.println("{RoomService}" + room);
                roomDao.updateRoom(room);
            } else { // id==null --> 添加
                room.setId(uuid()); // 设置 uuid
                System.out.println("{RoomService}" + room);
                roomDao.addRoom(room);
            }
        } catch (Exception e) {
            String[][] check = {{"room.uni_rm_no", "房间号重复"}, {"status", "状态只能是0-9"}};
            checkUniqueException(e, check);
        }
    }

    @Override
    public void delRoom(String id) {
        roomDao.delRoom(id);
    }

    @Override
    public Room getRoomById(String id) {
        Room room = roomDao.getRoomById(id);
        return room;
    }

    //+-----------------------------------RoomType--------------------------------------------+
    @Override
    public PageBean<RoomType> getRoomTypeList(PageParam param) {
        int size = param.getLimit();  //页大小
        int page = param.getPage();   //页码
        //{1}在查询之前先调用这个方法来准备拦截查询
        Page<Object> pageInfo = PageHelper.startPage(page, size, true);
        //{2}调用 dao 方法来查询
        List<RoomType> list = roomDao.getRoomTypeList(param.getKeywords());
        PageBean pBean = new PageBean();
        int total = (int) pageInfo.getTotal();
        //{3}生成 PageBean, 再封装信息到 pBean
        pBean.setCount(total);  //总记录数
        pBean.setData(list);    //用户列表
        pBean.setCode(0);     //状态码
        return pBean;
    }

    @Override
    public void saveRoomType(RoomType roomType) throws Exception {
        String id = roomType.getId();
        try {
            if (id != null) { // id!=null --> 更新
                System.out.println("{RoomService}" + roomType);
                roomDao.updateRoomType(roomType);
            } else { // id==null --> 添加
                roomType.setId(uuid()); // 设置 uuid
                System.out.println("{RoomService}" + roomType);
                roomDao.addRoomType(roomType);
            }
        } catch (Exception e) {
            String[][] check = {{"For input string", "输入数字啊SB"}};
            checkUniqueException(e, check);
        }
    }

    @Override
    public void delRoomType(String id) {
        roomDao.delRoomType(id);
    }

    @Override
    public RoomType getRoomTypeById(String id) {
        RoomType roomType = roomDao.getRoomTypeById(id);
        return roomType;
    }

    //+-----------------------------------Storey--------------------------------------------+
    @Override
    public PageBean<Storey> getStoreyList(PageParam param) {
        int size = param.getLimit();  //页大小
        int page = param.getPage();   //页码
        //{1}在查询之前先调用这个方法来准备拦截查询
        Page<Object> pageInfo = PageHelper.startPage(page, size, true);
        //{2}调用 dao 方法来查询
        List<Storey> list = roomDao.getStoreyList(param.getKeywords());
        PageBean pBean = new PageBean();
        int total = (int) pageInfo.getTotal();
        //{3}生成 PageBean, 再封装信息到 pBean
        pBean.setCount(total);  //总记录数
        pBean.setData(list);    //用户列表
        pBean.setCode(0);     //状态码
        return pBean;
    }

    @Override
    public void saveStorey(Storey storey) throws Exception {
        String id = storey.getId();
        try {
            if (id != null) { // id!=null --> 更新
                System.out.println("{RoomService}" + storey);
                roomDao.updateStorey(storey);
            } else { // id==null --> 添加
                storey.setId(uuid()); // 设置 uuid
                System.out.println("{RoomService}" + storey);
                roomDao.addStorey(storey);
            }
        } catch (Exception e) {
            String[][] check = {{"uni_floor", "楼层重复"}, {"floor", "楼层输入数字啊,sb"}};
            checkUniqueException(e, check);
        }
    }

    @Override
    public void delStorey(String id) {
        roomDao.delStorey(id);
    }

    @Override
    public Storey getStoreyById(String id) {
        Storey storey = roomDao.getStoreyById(id);
        return storey;
    }
}
